//
//  FeaturedViewController.m
//  Beauty
//
//  Created by Snow on 16/9/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "FeaturedViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfFeatureVCCollectionviewCell.h"
#import "NewProductCollectionViewCell.h"
#import "FeatureCollectionReusableView.h"
#import "NewProductViewController.h"
@interface FeaturedViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *arrOfPic;
@property (nonatomic, strong) NSMutableArray *arrayOfGood;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation FeaturedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    NSLog(@"%@", _special_id);
    [self handleData];
    [self createView];
}
- (void)handleData
{
    _arrayOfGood = [NSMutableArray array];
    _arrOfPic = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat: @"http://www.wantease.com/mobile1/?act=index&op=special&special_id=%@", _special_id];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"datas"];
        NSDictionary *first = [NSDictionary dictionaryWithDictionary:arr[0]];
        NSDictionary *diction = first[@"adv_list"];
        NSArray *arrFirst = diction[@"item"];
        NSDictionary *dict = arrFirst[0];
        [_arrOfPic addObject:dict];
        
        NSDictionary *second = arr[1];
        NSDictionary *dicSecond = second[@"goods"];
        NSArray *arrSecond = dicSecond[@"item"];
        for (NSDictionary *dic in arrSecond) {
            ModelOfFeatureVCCollectionviewCell *model = [[ModelOfFeatureVCCollectionviewCell alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfGood addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)createView
{
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.minimumLineSpacing = 10;
    _layout.minimumInteritemSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _layout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2, 180);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) collectionViewLayout:_layout];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[NewProductCollectionViewCell class] forCellWithReuseIdentifier:@"NewProductCollectionViewCell6"];
    [_collectionView registerClass:[FeatureCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FeatureCollectionReusableView"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfGood.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewProductCollectionViewCell6" forIndexPath:indexPath];
    ModelOfFeatureVCCollectionviewCell *model = [_arrayOfGood objectAtIndex:indexPath.row];
    [cell.imageOfGoods sd_setImageWithURL:[NSURL URLWithString:model.goods_image]];
    if (model.goods_name.length > 8) {
        cell.labelOfName.text = [model.goods_name substringToIndex:8];
    } else {
        cell.labelOfName.text = model.goods_name;
    }
    cell.labelOfPrice.text = [NSString stringWithFormat:@"¥ %@", model.goods_promotion_price];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FeatureCollectionReusableView *reuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FeatureCollectionReusableView" forIndexPath:indexPath];
    NSDictionary *dic = [_arrOfPic firstObject];
    [reuse.image sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
    return reuse;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = [_arrOfPic firstObject];
    CGFloat height = [dic[@"height"] floatValue];
    CGFloat width = [dic[@"width"] floatValue];
    CGFloat size = width / SCREEN_WIDTH;
    return CGSizeMake(SCREEN_WIDTH, height / size);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfFeatureVCCollectionviewCell *model = [_arrayOfGood objectAtIndex:indexPath.row];
    NewProductViewController *vc = [[NewProductViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.goods_id = model.goods_id;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
